package com.etoak.web.sys_role.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etoak.utils.ResultUtils;
import com.etoak.utils.ResultVo;
import com.etoak.web.sys_menu_role.entity.SaveMenuParm;
import com.etoak.web.sys_menu_role.service.SysMenRoleService;
import com.etoak.web.sys_role.entity.RoleParm;
import com.etoak.web.sys_role.entity.SelectItem;
import com.etoak.web.sys_role.entity.SysRole;
import com.etoak.web.sys_role.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author by 孙有泽
 * @Date 2025/2/15 11:47
 * @Description
 */
@RequestMapping("/api/role")
@RestController
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenRoleService sysMenRoleService;

    @PostMapping("/add")
    public ResultVo add(@RequestBody SysRole sysRole) {
        if (sysRoleService.save(sysRole)) {
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.error("新增失败");

    }

    @PutMapping ("/update")
    public ResultVo update(@RequestBody SysRole sysRole) {
        if (sysRoleService.updateById(sysRole)) {
            return ResultUtils.success("修改成功");
        }
        return ResultUtils.error("修改失败");
    }

    @DeleteMapping("/{id}")
    public ResultVo delete(@PathVariable String id) {
        //根据id假删除mybatis-plus
        if (sysRoleService.removeById(id)) {
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("删除失败");

    }
    @GetMapping("/getList")
    public ResultVo list(RoleParm parm) {
        //构造分组对象
        IPage<SysRole> page = new Page<>(parm.getCurrentPage(), parm.getPageSize());
      //  QueryWrapper<SysRole> objectQueryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysRole> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //构造查询条件
        if(StringUtils.isNotEmpty(parm.getRoleName())){
            objectLambdaQueryWrapper.like(SysRole::getName,parm.getRoleName());
        }
        objectLambdaQueryWrapper.orderByDesc(SysRole::getCreateTime);
        IPage<SysRole> page1 = sysRoleService.page(page, objectLambdaQueryWrapper);
        return ResultUtils.success("查询成功",page1);

    }
    @GetMapping("/selectList")
    public ResultVo selectList() {
        List<SysRole> list = sysRoleService.list();
        List<SelectItem> selectItems=new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>()).forEach(item->{
            SelectItem selectItem = new SelectItem();
            selectItem.setLabel(item.getName());
            selectItem.setValue(item.getId());
            selectItem.setCheck(false);
            selectItems.add(selectItem);
        });
        return ResultUtils.success("查询成功",selectItems);
    }
    @PostMapping("/saveRoleMenu")
    public ResultVo savaeRoleMenu(@RequestBody SaveMenuParm saveMenuParm){
        sysMenRoleService.saveRoleMenu(saveMenuParm);
        return ResultUtils.success("保存成功");
    }

}
